package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealContorller {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

   /* /**
     * 根据id修改菜品
     * @param id
     * @return
    @GetMapping("/{id}")
    public R<SetmealDish>  getData(@PathVariable Long id){

    }

    */

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
       log.info("套餐信息{}",setmealDto);
       setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");

    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {

        log.info("page={},pageSize={},name={}", page, pageSize, name);
        //A. 构造分页条件
        Page<Setmeal> pageInfo = new Page(page, pageSize);
        Page<Setmeal> dtoPage = new Page<>();
        //B. 构建搜索条件 - name进行模糊匹配  构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        //过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        //C. 构建排序条件 - 更新时间倒序排序
        queryWrapper.orderByDesc(Setmeal::getName);
        //D. 执行查询
        setmealService.page(pageInfo, queryWrapper);
        //拷贝对象
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();

        List<Setmeal> list = records.stream().map((item)->{
            SetmealDto setmealDto =new SetmealDto();
            //拷贝对象
            BeanUtils.copyProperties(item,setmealDto);

            // 分类id
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);

            if (category != null ){
                //分类名称
                String catrgoryName = category.getName();
                setmealDto.setCategoryName(catrgoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        //E. 组装结果并返回
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    /**
     * 根据id删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public  R<String > delete (@RequestBody List<Long> ids){
        log.info("ids{}",ids);

        setmealService.removeWithDish(ids);
       return R.success("套餐删除成功");
    }

    /**
     * 根据id修改套餐状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String>  status(@PathVariable("status") Integer status,@RequestBody List<Long> ids){
        setmealService.updateSetmealStatusByIds(status,ids);
        return R.success("状态修改成功");
    }
}
